package com.moleq.service;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.moleq.db.DBError;
import com.moleq.db.DatabaseHelper;
import com.moleq.db.DatabaseSchema;
import com.moleq.dialog.MessageBox;
import com.moleq.models.GlobalInfo;
import com.moleq.models.ItemDiscountSetting;
import com.moleq.models.SetFunction;

public class ItemDiscountSettingService {
	private DatabaseHelper DBHelper;
	private String errorString;
	private Context mContext;

	public ItemDiscountSettingService() {

	}

	public ItemDiscountSettingService(Context context) {
		mContext = context;
		this.DBHelper = new DatabaseHelper(GlobalInfo.getInstance().ShareContext);
	}
	
	public List<ItemDiscountSetting> FindAll(){
		List<ItemDiscountSetting> obj = new ArrayList<ItemDiscountSetting>();
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select * from "+ DatabaseSchema.ItemDiscountSetting.SCHEMA_NAME +" order by ds_no",null);
		if(cursor == null){
			MessageBox.show(mContext,DBError.error);
			DBError.error = "";
			return obj;
		}
		while(cursor.moveToNext()){
			int ds_no  = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.ItemDiscountSetting.DS_NO));
			String ds_desc  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemDiscountSetting.DS_DESC));
			String ds_type  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemDiscountSetting.DS_TYPE));
			double ds_value  = cursor.getDouble(cursor.getColumnIndex(DatabaseSchema.ItemDiscountSetting.DS_VALUE));
			String create_datetime  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemDiscountSetting.CREATE_TIME));
			String update_datetime  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemDiscountSetting.UPDATE_TIME));
			boolean ds_follow_item = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemDiscountSetting.DS_FOLLOW_ITEM)).equals("1")?true:false;
			boolean ds_tax1 = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemDiscountSetting.DS_TAX1)).equals("1")?true:false;
			boolean ds_tax2 = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemDiscountSetting.DS_TAX2)).equals("1")?true:false;
			boolean ds_tax3 = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemDiscountSetting.DS_TAX3)).equals("1")?true:false;
			boolean ds_tax4 = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemDiscountSetting.DS_TAX4)).equals("1")?true:false;
			boolean ds_tax5 = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemDiscountSetting.DS_TAX5)).equals("1")?true:false;
			boolean ds_tax6 = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemDiscountSetting.DS_TAX6)).equals("1")?true:false;
			boolean ds_foodstamp = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemDiscountSetting.DS_FOODSTAMP)).equals("1")?true:false;
			
			ItemDiscountSetting id = new ItemDiscountSetting(ds_no, ds_type, ds_desc, ds_value, create_datetime, update_datetime, 
					ds_follow_item, ds_tax1, ds_tax2, ds_tax3, ds_tax4, ds_tax5, ds_tax6, ds_foodstamp);
			id.setFlag("O");
			obj.add(id);
		}
		cursor.close();
		return obj;
	}
	
	public List<SetFunction> FindItemDisNoDesc(){
		List<SetFunction> obj = new ArrayList<SetFunction>();
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select ds_no,ds_desc from "+ DatabaseSchema.ItemDiscountSetting.SCHEMA_NAME +" order by ds_no",null);
		if(cursor == null){
			MessageBox.show(mContext,DBError.error);
			DBError.error = "";
			return obj;
		}
		obj.add(new SetFunction("0", "Manual (%)"));
		obj.add(new SetFunction("-1", "Manual (Amount)"));
		while(cursor.moveToNext()){
			String ds_no  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemDiscountSetting.DS_NO));
			String ds_desc  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemDiscountSetting.DS_DESC));
			
			obj.add(new SetFunction(ds_no, ds_desc));
		}
		cursor.close();
		return obj;
	}
	
	public List<ItemDiscountSetting> FindItemDiscountByType(String type){
		List<ItemDiscountSetting> obj = new ArrayList<ItemDiscountSetting>();
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select * from "+ DatabaseSchema.ItemDiscountSetting.SCHEMA_NAME +" where ds_type = '"+ type +"' order by ds_no",null);
		if(cursor == null){
			MessageBox.show(mContext,DBError.error);
			DBError.error = "";
			return obj;
		}
		while(cursor.moveToNext()){
			int ds_no  = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.ItemDiscountSetting.DS_NO));
			String ds_desc  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemDiscountSetting.DS_DESC));
			String ds_type  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemDiscountSetting.DS_TYPE));
			double ds_value  = cursor.getDouble(cursor.getColumnIndex(DatabaseSchema.ItemDiscountSetting.DS_VALUE));
			String create_datetime  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemDiscountSetting.CREATE_TIME));
			String update_datetime  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemDiscountSetting.UPDATE_TIME));
			boolean ds_follow_item = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemDiscountSetting.DS_FOLLOW_ITEM)).equals("1")?true:false;
			boolean ds_tax1 = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemDiscountSetting.DS_TAX1)).equals("1")?true:false;
			boolean ds_tax2 = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemDiscountSetting.DS_TAX2)).equals("1")?true:false;
			boolean ds_tax3 = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemDiscountSetting.DS_TAX3)).equals("1")?true:false;
			boolean ds_tax4 = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemDiscountSetting.DS_TAX4)).equals("1")?true:false;
			boolean ds_tax5 = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemDiscountSetting.DS_TAX5)).equals("1")?true:false;
			boolean ds_tax6 = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemDiscountSetting.DS_TAX6)).equals("1")?true:false;
			boolean ds_foodstamp = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemDiscountSetting.DS_FOODSTAMP)).equals("1")?true:false;
			
			ItemDiscountSetting id = new ItemDiscountSetting(ds_no, ds_type, ds_desc, ds_value, create_datetime, update_datetime, 
					ds_follow_item, ds_tax1, ds_tax2, ds_tax3, ds_tax4, ds_tax5, ds_tax6, ds_foodstamp);
			id.setFlag("O");
			obj.add(id);
		}
		cursor.close();
		return obj;
	}
	
	public ItemDiscountSetting Find(int no){
		ItemDiscountSetting obj = null;
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select * from "+ DatabaseSchema.ItemDiscountSetting.SCHEMA_NAME +" where ds_no = "+no,null);
		if(cursor == null){
			MessageBox.show(mContext,DBError.error);
			DBError.error = "";
			return obj;
		}
		while(cursor.moveToNext()){
			
			int ds_no  = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.ItemDiscountSetting.DS_NO));
			String ds_desc  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemDiscountSetting.DS_DESC));
			String ds_type  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemDiscountSetting.DS_TYPE));
			double ds_value  = cursor.getDouble(cursor.getColumnIndex(DatabaseSchema.ItemDiscountSetting.DS_VALUE));
			String create_datetime  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemDiscountSetting.CREATE_TIME));
			String update_datetime  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemDiscountSetting.UPDATE_TIME));
			boolean ds_follow_item = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemDiscountSetting.DS_FOLLOW_ITEM)).equals("1")?true:false;
			boolean ds_tax1 = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemDiscountSetting.DS_TAX1)).equals("1")?true:false;
			boolean ds_tax2 = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemDiscountSetting.DS_TAX2)).equals("1")?true:false;
			boolean ds_tax3 = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemDiscountSetting.DS_TAX3)).equals("1")?true:false;
			boolean ds_tax4 = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemDiscountSetting.DS_TAX4)).equals("1")?true:false;
			boolean ds_tax5 = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemDiscountSetting.DS_TAX5)).equals("1")?true:false;
			boolean ds_tax6 = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemDiscountSetting.DS_TAX6)).equals("1")?true:false;
			boolean ds_foodstamp = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemDiscountSetting.DS_FOODSTAMP)).equals("1")?true:false;
			
			obj = new ItemDiscountSetting(ds_no, ds_type, ds_desc, ds_value, create_datetime, update_datetime, 
					ds_follow_item, ds_tax1, ds_tax2, ds_tax3, ds_tax4, ds_tax5, ds_tax6, ds_foodstamp);
		}
		cursor.close();
		return obj;
	}
	
	public boolean updateSetItemDiscountNo(int discount_no){
		boolean check = true;
		errorString = DBHelper.execSQL("Update "+ DatabaseSchema.ItemDiscountSetting.SCHEMA_NAME +" set ds_no = ds_no - 1 where ds_no > " + discount_no);
		if (errorString.length() > 0) {
			check = false;
		}
		return check;
	}
	
	public boolean insertItemDiscountSetting(ItemDiscountSetting obj){
		boolean check = true;
		String sql,desc,type,follow,tax1,tax2,tax3,tax4,tax5,tax6,fs;
		double value;
		desc = obj.getDs_desc();
		type = obj.getDs_type();
		value = obj.getDs_value();
		follow = obj.isDs_follow_item()?"1":"0";
		tax1 = obj.isDs_tax1()?"1":"0";
		tax2 = obj.isDs_tax2()?"1":"0";
		tax3 = obj.isDs_tax3()?"1":"0";
		tax4 = obj.isDs_tax4()?"1":"0";
		tax5 = obj.isDs_tax5()?"1":"0";
		tax6 = obj.isDs_tax6()?"1":"0";
		fs = obj.isDs_foodstamp()?"1":"0";
		
		sql = "Insert into "+ DatabaseSchema.ItemDiscountSetting.SCHEMA_NAME +" values";
		sql += "(null,'"+ desc +"','"+ type +"',"+ value +",strftime('%Y%m%d%H%M%S','now'),strftime('%Y%m%d%H%M%S','now')," +
				"'"+ follow +"','"+ tax1 +"','"+ tax2 +"','"+ tax3 +"','"+ tax4 +"'," +
						"'"+ tax5 +"','"+ tax6 +"','"+ fs +"')";
		errorString = DBHelper.execSQL(sql);
		if (errorString.length() > 0) {
			check = false;
		}
		return check;
	}
	
	public boolean updateItemDiscountSetting(ItemDiscountSetting obj){
		boolean check = true;
		String sql,desc,type,follow,tax1,tax2,tax3,tax4,tax5,tax6,fs;
		double value;
		int no = obj.getDs_no();
		desc = obj.getDs_desc();
		type = obj.getDs_type();
		value = obj.getDs_value();
		follow = obj.isDs_follow_item()?"1":"0";
		tax1 = obj.isDs_tax1()?"1":"0";
		tax2 = obj.isDs_tax2()?"1":"0";
		tax3 = obj.isDs_tax3()?"1":"0";
		tax4 = obj.isDs_tax4()?"1":"0";
		tax5 = obj.isDs_tax5()?"1":"0";
		tax6 = obj.isDs_tax6()?"1":"0";
		fs = obj.isDs_foodstamp()?"1":"0";
		
		sql = "UPDATE "+ DatabaseSchema.ItemDiscountSetting.SCHEMA_NAME +" set ";
		sql += "ds_desc = '"+ desc +"',ds_type = '"+ type +"',ds_value = "+ value +"," +
				"update_datetime = strftime('%Y%m%d%H%M%S','now'),ds_follow_item = '"+ follow +"'," +
				"ds_tax1 = '"+ tax1 +"',ds_tax2 = '"+ tax2 +"',ds_tax3 = '"+ tax3 +"',ds_tax4 = '"+ tax4 +"'," +
				"ds_tax5 = '"+ tax5 +"',ds_tax6 = '"+ tax6 +"',ds_foodstamp = '"+ fs +"' where ds_no = "+ no;
		errorString = DBHelper.execSQL(sql);
		if (errorString.length() > 0) {
			check = false;
		}
		return check;
	}
	
	public boolean deleteItemDiscountSetting(int no){
		boolean check = true;
		String sql = "delete from "+ DatabaseSchema.ItemDiscountSetting.SCHEMA_NAME +" where ds_no = " + no;
		errorString = DBHelper.execSQL(sql);
		if (errorString.length() > 0) {
			check = false;
		}
		return check;
	}
	
	public String getItemDiscountName(String no){
		String obj = null;
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select ds_desc from "+ DatabaseSchema.ItemDiscountSetting.SCHEMA_NAME +" where ds_no = "+no,null);
		if(cursor == null){
			MessageBox.show(mContext,DBError.error);
			DBError.error = "";
			return obj;
		}
		if(cursor.moveToNext()){
			obj = cursor.getString(cursor.getColumnIndex(DatabaseSchema.ItemDiscountSetting.DS_DESC));
	
		}
		cursor.close();
		return obj;
	}
	
	public boolean PriceSettingHaveItemDiscount(int discountNo){
		boolean result = false;
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select ps_discnt_link from t_price_setting where ps_discnt_link = " + discountNo, null);
		if(cursor == null){
			MessageBox.show(mContext,DBError.error);
			DBError.error = "";
			return result;
		}
		if (cursor.moveToNext()) {
			result = true;
		}
		cursor.close();
		return result;
	}
	
	public boolean MMSettingHaveItemDiscount(int discountNo){
		boolean result = false;
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select F01 from t_mix_match where mm_no in (select ms_no from t_mix_match_setting where ms_discnt_link = "+ discountNo +")", null);
		if(cursor == null){
			MessageBox.show(mContext,DBError.error);
			DBError.error = "";
			return result;
		}
		if (cursor.moveToNext()) {
			result = true;
		}
		cursor.close();
		return result;
	}
	
	public boolean FuncPanelHaveItemDiscount(int discountNo){
		boolean result = false;
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select tp_parameter2 from t_function_panel where tp_func_key = 31 and tp_parameter2 = " + discountNo, null);
		if(cursor == null){
			MessageBox.show(mContext,DBError.error);
			DBError.error = "";
			return result;
		}
		if (cursor.moveToNext()) {
			result = true;
		}
		cursor.close();
		return result;
	}
	
	public int getMaxDisNo(){
		int order = 0;
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select Max(ds_no) from " + DatabaseSchema.ItemDiscountSetting.SCHEMA_NAME, null);
		if(cursor == null){
			MessageBox.show(mContext,DBError.error);
			DBError.error = "";
			return order;
		}
		if (cursor.moveToNext()) {
			order = cursor.getInt(0);
		}
		cursor.close();
		return order;
	}
}
